import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home.vue'
import studentList from './../views/student/studentList.vue'
import studentAdd from '../views/student/studentAdd.vue' 
import subject from '../views/subject/subject.vue'
import classesList from '../views/classes/classesList.vue'
import classesAdd from '../views/classes/classesAdd.vue'
import studentUpdate from '../views/student/studentUpdate.vue'
import login from '../views/login/login.vue'
import register from '../views/register/register.vue'
import subjectSUpdate from "../views/subject/subjectSUpdate.vue"
import subjectAdd from '../views/subject/subjectAdd.vue'
import charts from '../views/charts/charts.vue'
//引入elementui中的弹窗
import {MessageBox} from 'element-ui'
import api from '@/http/api.js'
//引入仓库对象
import store from '@/store/index.js'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/home'
  },
  {
    path:'/login',
    name:'login',
    component:login
  },
  {
    path:'/register',
    name:'register',
    component:register
  },
  {
    path: '/home',
    component: Home,
    beforeEnter: async (to, from, next) => {
         //判断本地
        const token= localStorage.token
        if(token){
        const {data}  =  await  api.login.getUserInfo()
        console.log(data);
              if(data.code){
                //保存用户信息：vuex或者本地
                store.commit('SET_USERINFO',data.data)
                next()
              }
        }else{
          MessageBox.alert('你还未登录，请先登录', {
            confirmButtonText: '确定',
            callback: action => {
                next('/login')
            }
          });
        }
  
    },
    children:[
      //与/home同时出现，name和home同名
      {
        path:'',
        component:charts,
        name:'home'
      },
      {
      path:'studentList',
      name:'studentList',
      component:studentList
    },
    {
      path:'studentAdd',
      name:'studentAdd',
      component:studentAdd
    },
    {
      path:'subject',
      name:'subject',
      component:subject
    },
    {
      path:'classesList',
      name:'classesList',
      component:classesList
    },
    {
      path:'classesAdd',
      name:'classesAdd',
      component:classesAdd
    },
    {
      path:'studentUpdate/:id',
      name:'studentUpdate',
      component:studentUpdate,
    
    },
    {
      path:'subjectSUpdate/:id',
      name:'subjectSUpdate',
      component:subjectSUpdate,
    
    },
    {
      path:'subjectAdd',
      name:'subjectAdd',
      component:subjectAdd,
    
    },

    
  ]
  },
]


const router = new VueRouter({
  routes
})



export default router
